/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.application;

import java.util.HashMap;
import org.balisunrise.common.ComponentManager;

/**
 * Armazena ComponentManager para diversos tipos de dados.
 * 
 * @author Glauco
 */
public class ComponentManagers {

    private HashMap<Class, ComponentManager> managers;

    /**
     * Construtor.
     */
    public ComponentManagers(){
        managers = new HashMap<Class, ComponentManager>();
    }

    /**
     * Adiciona um ComponentManager.
     * Se o tipo de dados do ComponentManager já existir, o anterior é
     * descartado e será utilizado este daqui em diante.
     * @param cm ComponentManager
     */
    public void addComponentManager(ComponentManager cm){

        Class cmClass = cm.getType();

        if(managers.containsKey(cmClass))
            managers.remove(cmClass);

        managers.put(cmClass, cm);
    }

    /**
     * Pega o ComponentManager do tipo de dado.<br>
     * Caso nenhum ComponentManager fora adicionado para este tipo de dado é
     * retornado nulo.
     *
     * @param type Tipo de Dado.
     * @return The ComponentManager.
     */
    public ComponentManager getComponentManager(Class type){

        if(managers.containsKey(type))
            return managers.get(type);

        return null;
    }
    
}
